// --信息填报页面--
var app = getApp();
var util = require('../../../utils/util.js');
// var vaddress
Page({

  /**
   * 页面的初始数据
   */

  data: {
    btnEn: false,
    twocode_en:true,
    modalHidden: true,//是否隐藏对话框
    // modalHidden1: true,//是否隐藏对话框
    modatext: "请注意：体温情况异常,请确认与实际情况相符！",
  },
  onLoad: function () {
    this.findOpenid();
    this.sex_radio();
    this.out_in_radio();
    this.sfxiaoqu_person_carf();
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../../chooseLib/chooseLib',
      })
      return
    }
  },
  start:function() {
    wx.cloud.callFunction({
      name: "person_out",///////////////传参数
      data: {
        modify_id: res.data[0]._id,
        outtimev: outtimev
      },
      success(res) {
        // console.log("读取成功cloud_data", res.result.data)
        wx.showToast({
          title: '离开登记成功',
          icon: 'success',
        })

      },
      fail(res) {
        console.log("读取失败getusers", res)
      }
    })
  },
  twocode_en_false :function() {
// console.log("修改")
    this.setData({
      twocode_en: true,
      btnEn: false,
    })
  },
  selectlist: function (e) {
    var arrwc = [], arrcar = [], arrsex = [];  //存放选中id的数组
    var json;
    var that =this;
    // console.log('e.currentTarget.dataset.index.sfxiaoqu_person_car', e.currentTarget.dataset.index.sfxiaoqu_person_car)
    // console.log('out_or_inv:this.data.sfxiaoqu_person_car.length', e.currentTarget.dataset.index.lxbx.length) 
    // console.log('this.data.sfxiaoqu_person_car', this.data.sfxiaoqu_person_car)

  

    if (e.currentTarget.dataset.index.sfxiaoqu_person_car) {
      for (let i = 0; i < this.data.sfxiaoqu_person_car.length; i++) {
        if (e.currentTarget.dataset.index.sfxiaoqu_person_car.includes(this.data.sfxiaoqu_person_car[i].name)) {
          // 全选获取选中的值
          json = { "checked": true, "name": this.data.sfxiaoqu_person_car[i].name, "value": this.data.sfxiaoqu_person_car[i].value }
          // console.log('out_or_inv:true' + i, json) 
        }
        else {
          json = { "checked": false, "name": this.data.sfxiaoqu_person_car[i].name, "value": this.data.sfxiaoqu_person_car[i].value }
          // console.log('out_or_inv:false' + i, json)        
        }
        arrcar.push(json);
      }
    }
    else {
      for (let i = 0; i < this.data.sfxiaoqu_person_car.length; i++) {

        json = { "checked": false, "name": this.data.sfxiaoqu_person_car[i].name, "value": this.data.sfxiaoqu_person_car[i].value }
        // console.log('out_or_inv:false' + i, json)        

        arrcar.push(json);
      }
    }

    // console.log(e.currentTarget.dataset.index.sfwc)
    if (e.currentTarget.dataset.index.sex) {
      for (let i = 0; i < this.data.sex_radio.length; i++) {
        if (e.currentTarget.dataset.index.sex.includes(this.data.sex_radio[i].name)) {
          // 全选获取选中的值
          json = { "checked": true, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          // console.log('out_or_inv:true' + i, json) 
        }
        else {
          json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          // console.log('out_or_inv:false' + i, json)        
        }
        arrsex.push(json);
      }
    }
    else {
      for (let i = 0; i < this.data.sex_radio.length; i++) {
        json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
        arrsex.push(json);
      }
    }
    if (e.currentTarget.dataset.index.out_or_inv) {
      for (let i = 0; i < this.data.out_or_inv.length; i++) {
        if (e.currentTarget.dataset.index.out_or_inv.includes(this.data.out_or_inv[i].name)) {
          // 全选获取选中的值
          json = { "checked": true, "name": this.data.out_or_inv[i].name, "value": this.data.out_or_inv[i].value }
          //  console.log('out_or_inv:true' + i, json) 
        }
        else {
          json = { "checked": false, "name": this.data.out_or_inv[i].name, "value": this.data.out_or_inv[i].value }
        }
        arrwc.push(json);
      }
    } else {
      for (let i = 0; i < this.data.out_or_inv.length; i++) {
        json = { "checked": false, "name": this.data.out_or_inv[i].name, "value": this.data.out_or_inv[i].value }
        arrwc.push(json);
      }
    }
    this.setData({
      modalHidden: !this.data.modalHidden,
      name: e.currentTarget.dataset.index.name,
      sex_radio: arrsex,
      age: e.currentTarget.dataset.index.age,
      sfz: e.currentTarget.dataset.index.sfz,
      phone: e.currentTarget.dataset.index.phone,
      sfxiaoqu_person_car: arrcar,
      car_number: e.currentTarget.dataset.index.car_number,
      address: e.currentTarget.dataset.index.address,
      out_in_mesage: e.currentTarget.dataset.index.out_in_mesage,
      out_where: e.currentTarget.dataset.index.out_where,
      out_or_inv: arrwc,
      fromwhere_time: e.currentTarget.dataset.index.fromwhere_time,
      fromwhere: e.currentTarget.dataset.index.fromwhere,
      memo: e.currentTarget.dataset.index.memo,
    })
    app.globalData.input_id = e.currentTarget.dataset.index._id
    if (
      e.currentTarget.dataset.index.name && e.currentTarget.dataset.index.sex && e.currentTarget.dataset.index.age && e.currentTarget.dataset.index.sfz &&
      e.currentTarget.dataset.index.sfxiaoqu_person_car 
      && e.currentTarget.dataset.index.address && e.currentTarget.dataset.index.out_in_mesage &&
      e.currentTarget.dataset.index.out_or_inv && e.currentTarget.dataset.index.out_where && e.currentTarget.dataset.index.phone) {
      
      // console.log('信息完整')
      this.setData({
        twocode_en: false,
        btnEn:true,
      })
      }
    


  },
  
  createTwoCode :function (){
    wx.redirectTo({
        url: '/pages/createTwoCode/createTwoCode'
      })
  },
  sex_radio: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('sex').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.sex)
        that.setData({
          sex_radio: res.data.sex,
        })
      }
    })
  },
  sfxiaoqu_person_carf: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('true_false').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.sex)
        that.setData({
          sfxiaoqu_person_car: res.data.filde,
        })
      }
    })
  },
  out_in_radio: function (e) {

    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('true_false').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.lxbx)
        that.setData({
          out_or_inv: res.data.filde,
        })
      }
    })
  },
  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getPhone',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      this.setData({
        phone: res.result,
      })
    }).catch(err => {
    });
  },
  //事件处理函数
  // bindViewTap: function () {
  //   this.setData({
  //     modalHidden: !this.data.modalHidden,

  //   })
  // },
  //确定按钮点击事件
  modalBindaconfirm: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
    })

  },
  //取消按钮点击事件
  // modalBindcancel: function () {
  //   this.setData({
  //     modalHidden: !this.data.modalHidden,
  //   })
  // },

  
  findOpenid: function () {
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        app.globalData.openid = res.result.openid
        db.collection('base').where({
          _openid: app.globalData.openid
        }).limit(8)
          .get()          
          .then(res => {

            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              // console.log('res.data', res.data)
              this.setData({
                list_n: res.data,
                modalHidden: false,
              })
              // console.log('findopenid', res.data)
            }
            
            else {
              console.log('没查询到数据', res.data)
            }
          })

      }
    })


  },


  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)

    this.setData({ btnEn: true })
    if (
      e.detail.value.name && e.detail.value.age && e.detail.value.sfz && 
      e.detail.value.sfxiaoqu_person_car && e.detail.value.address && e.detail.value.out_in_mesage && 
      e.detail.value.out_or_inv && e.detail.value.out_where ) {
      if (e.detail.value.phone) {
        const db = wx.cloud.database()

        db.collection('base').add({
          data: {
            name: e.detail.value.name,
            sex: e.detail.value.sex,
            sfz: e.detail.value.sfz,
            age: e.detail.value.age,
            phone: e.detail.value.phone,
            sfxiaoqu_person_car: e.detail.value.sfxiaoqu_person_car,
            car_number: e.detail.value.car_number,
            address: e.detail.value.address,
            out_in_mesage: e.detail.value.out_in_mesage,
            out_where: e.detail.value.out_where,
            out_or_inv: e.detail.value.out_or_inv,
            fromwhere_time: e.detail.value.fromwhere_time,
            fromwhere: e.detail.value.fromwhere,
            memo: e.detail.value.memo,
            form: "小区测温",
            addtime: util.formatTime(new Date()),
            // 测温时间:0,


          }
          ,
          success: res => {
            // 在返回结果中会包含新创建的记录的 _id                  
            // wx.showToast({
            //   title: '新增记录成功',
            // })

            // console.log('添加数据02', res._id)
            app.globalData.input_id = res._id
            // app.globalData.input_link="station"
            wx.redirectTo({
              url: '/pages/createTwoCode/createTwoCode'
            })
            this.setData({ btnEn: false })
          },
          fail: err => {
            wx.showToast({
              icon: 'none',
              title: '新增记录失败,请找当值人员手工登记！'
            })
            // console.error('[数据库] [新增记录] 失败：', err)
          }
        })

      }
      else {
        // console.log('请更改地址信息：', e.detail.value)
        wx.showToast({
          icon: 'loading',
          title: '请获取手机号',
        })
        this.setData({ btnEn: false })
      }
    }
    else {
      // console.log('form发生了submit事件，携带数据为：', e.detail.value)
      wx.showToast({
        icon: 'loading',
        title: '请填全红字信息',
      })
      this.setData({ btnEn: false })
    }
    // this.setData({ btnEn: false })
  },



})